$().ready(function() {
    //省
    $('#adre').citys({province:"请选择"});
	validateRule();
    select();
    $("#type").change(function () {
        if ($(this).val() == 0){
            $(".company").css("display" , "block");
        }else {
            $(".company").css("display" , "none");
        }

        if ($(this).val() == 2){
            $(".school").css("display" , "block");
        }else {
            $(".school").css("display" , "none");
        }
    });
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
//分页下拉框
function select() {
    $('#selectPage').selectPage({
        showField : 'mobile',
        keyField : 'id',
        data :'/sys/user/list',
        searchField:'name',
        //格式化显示项目，提供源数据进行使用
        formatItem : function(data){
            return data.name + '(' + data.mobile + ')';
        },
        eAjaxSuccess : function(d){
            var result;
            if(d) result = d.data;
            else result = undefined;
            return result;
        }
    });
}
function save() {
    /*if($("#selectPage").val()==''){
        layer.msg("负责人不能为空");
        return false
    }*/
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/organizationNews/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
        rules : {
            type : {
                required : true
            },
            name : {
                required : true
            },
        },
        messages : {
            type : {
                required : icon + "请选择机构类型"
            },
            name : {
                required : icon + "请输入机构名称"
            },
        }
	})
}

layui.use('upload', function () {
    var upload = layui.upload;
    upload.render({
        elem: '#btnUpload',
        url: '/common/sysFile/upload',
        accept: 'images', //只允许上传图片
        acceptMime: 'image/*', //只筛选图片
        size:1024,
        done: function (r) {
            $("#orgPhoto").val(r.data);
            $("#img").attr("src",r.data)
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});

layui.use('upload', function () {
    var upload = layui.upload;
    upload.render({
        elem: '#btnUpload1',
        url: '/common/sysFile/upload',
        accept: 'images', //只允许上传图片
        acceptMime: 'image/*', //只筛选图片
        size:1024,
        done: function (r) {
            $("#cachet").val(r.data);
            $("#img1").attr("src",r.data)
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});